<?php
namespace Home\Model;
use Think\Model;
// +----------------------------------------------------------------------
// | Date:2016年12月20日
// +----------------------------------------------------------------------
// | Author: EK_熊<1439527494@qq.com>
// +----------------------------------------------------------------------
// | Description: 此文件作用于****
// +----------------------------------------------------------------------
class ProductModel extends Model{
    
    
    
    function getListGroup($where=[],$order='',$limit=9){
        $order = empty($order) ? 'listorder desc,published desc' : $order ;
        $where['status'] = 1;
        
        $field = 'id,summary,title,image_id,t_lx,group_id,grp.total,t_jx,keywords';
        $data = $this->alias('pro')->where($where)
            ->join('LEFT JOIN __PRODUCT_GROUP__ grp ON grp.grp_id = pro.group_id')
            ->order($order)
            ->field($field)
            ->limit($limit)
            ->select();
        
        if (!$data) return null;
        //获取封面图
        return $this->getCover($data);
    }
    
    function getList($where=[],$order='published desc',$limit=9){
        $where['status'] = 1;
        $field = 'id,summary,title,image_id,t_lx,t_jx,keywords,group_id';
        $data = $this->alias('pro')->where($where)->order($order)->field($field)->limit($limit)->select();
        if (!$data) return null;
        //获取封面图
        return $this->getCover($data);        
    }
    
    function getCover($data=[]){
        
        foreach ($data as $k=>$v) {
            $image_id_ary = explode(',',$v['image_id']);
            $imageBox[] = $image_id_ary[0];
        }
        
        if ($imageBox) {
            $imageInfo = M('images')->where(['id'=>['in',$imageBox]])->getField('id,savepath',true);
            
            //拼接封面路径，返回数据
            foreach ($data as $k=>$v){
                $image_id_ary = explode(',',$v['image_id']);
                $data[$k]['cover'] = $imageInfo[$image_id_ary[0]];
            }
        }
        
        return $data;
    }
    
    
        
    
}